package com.oyjd.beauty.business.recommand.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.oyjd.beauty.bean.po.Goods;
import com.oyjd.beauty.bean.po.GoodsBody;
import com.oyjd.beauty.bean.po.GoodsKid;
import com.oyjd.beauty.bean.po.GoodsType;
import com.oyjd.beauty.bean.po.Recommand;
import com.oyjd.beauty.business.goods.dao.GoodsDao;
import com.oyjd.beauty.business.goods.vo.GoodsVOEx;
import com.oyjd.beauty.fw.action.Page;
import com.oyjd.beauty.fw.dao.BaseDao;

/**
 * 
 * 店长推荐数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class recommandDao extends BaseDao {
	
	private GoodsDao dao;
	
	@Autowired
	public void setDao(GoodsDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取某类型的最大排序码
	 * 
	 * @param type
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public int getMaxOrderNo() {
		int max = 0;
		String hql = "select max(orderNo) from " + Recommand.class.getName();
		List<Integer> t = (List<Integer>) queryByHql(hql);
		if (t.size() > 0) {
			Integer n = t.get(0);
			if (null != n) {
				max = n.intValue();
			}
		}
		return max;
	}

	/**
	 * 获取分类排序的列表
	 * 
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<GoodsVOEx> getList(Page<GoodsVOEx> page) {
		String hqlResult = "select new " + GoodsVOEx.class.getName() + GoodsVOEx.STRUCT;
		String hqlCount = "select count(goods)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Goods.class.getName()).append(" goods,");
		sb.append(GoodsType.class.getName()).append(" gt,");
		sb.append(Recommand.class.getName()).append(" r");
		sb.append(" where goods.status=0 and goods.goodsTypeId=gt.goodsTypeId");
		sb.append(" and r.goodsId=goods.goodsId");
		sb.append(" order by r.orderNo desc");

		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

	/**
	 * 获取差异的列表
	 * 
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<GoodsVOEx> getLeftList(Page<GoodsVOEx> page) {
		String hqlResult = "select new " + GoodsVOEx.class.getName() + GoodsVOEx.STRUCT;
		String hqlCount = "select count(goods)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Goods.class.getName()).append(" goods,");
		sb.append(GoodsType.class.getName()).append(" gt");
		sb.append(" where goods.status=0 and goods.goodsTypeId=gt.goodsTypeId");
		sb.append(" and not exists(select r.id from " + Recommand.class.getName() + " r where r.goodsId=goods.goodsId)");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		for (String key : paras.keySet()) {
			if ("title".equals(key)) {
				sb.append(" and goods.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			} else if ("goodsTypeId".equals(key)) {// 商品分类
				List<String> types = dao.getType((String) paras.get(key)[0]);
				sb.append(" and (");
				for (int i = 0; i < types.size(); i++) {
					sb.append(0 == i ? "" : " or ");
					sb.append("goods.").append(key).append(" = ?");
					ps.add(types.get(i));
				}
				sb.append(")");
			} else if ("kid".equals(key)) {// 性格
				sb.append(" and exists(select k.id from " + GoodsKid.class.getName() + " k where k.goodsId=goods.goodsId and k.kidId=?)");
				ps.add(paras.get(key)[0]);
			} else if ("body".equals(key)) {// 体型
				sb.append(" and exists(select g.id from " + GoodsBody.class.getName() + " g where g.goodsId=goods.goodsId and g.bodyId=?)");
				ps.add(paras.get(key)[0]);
			}
		}
		sb.append(" order by goods.lastUpdateTime desc");

		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

}
